package com.gistone.commis.controller;

import com.gistone.commis.database.GetBySqlMapper;
import com.gistone.commis.entity.*;
import com.gistone.commis.mapper.*;
import com.gistone.commis.query.QueryHouse;
import com.gistone.commis.query.QueryPerson;
import com.gistone.commis.query.QueryProperty;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("index")
public class IndexController {
	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	private GetBySqlMapper getBySqlMapper;
	@Autowired
	private HouseMapper houseMapper;

	@Autowired
	private PersonMapper personMapper;
	@Autowired
	private PropertyMapper propertyMapper;
	@Autowired
	private FundMapper fundMapper;

	@Autowired
	private OfficeSuppliesMapper officeSuppliesMapper;


	@RequestMapping(value = "home")
	public void pageSelect(HttpServletRequest request,HttpServletResponse response) throws IOException{

		HttpSession httpSession = request.getSession();
		User user =(User)httpSession.getAttribute("userInfo");
		logger.info("用户信息："+user.toString());


		JSONObject index_result= new JSONObject();


		int community_id = user.getCommunity().getCommunityId();
		logger.info("community_id:"+community_id);

		

		
    	String sql = "select * from tb_community_info where community_id = " +community_id+" order by pk_id desc";
		


    	List<Map> index_List = this.getBySqlMapper.findRecords(sql);
		QueryHouse house =new QueryHouse();
		house.setCommunityId(community_id);
		house.setIsDelete(0);
		double sumHouseArea= houseMapper.sumSelective(house);
		System.out.println(sumHouseArea);
		index_result.put("sumHouseArea",sumHouseArea);//社区房屋面积
		QueryProperty property = new QueryProperty();
		property.setCommunityId(community_id);
		property.setIsDelete(0);
		property.setPropertyState(1);//在用
		int propertyNum=0;
		List<Property> propertyList=propertyMapper.selectSelective(property);
		if(null!=propertyList){
			propertyNum =propertyList.size();

		}
		index_result.put("propertyNum",propertyNum);

		QueryPerson person = new QueryPerson();
		person.setCommunityId(community_id);
		person.setIsDelete(0);

		int personNum=0;
		List<Person> personList = personMapper.selectSelective(person);
		if(null!=personList){
			personNum =personList.size();

		}
		index_result.put("personNum",personNum);

		Fund fund = new Fund();
		fund.setCommunityId(community_id);
		fund.setIsDelete(0);
		int fundNum=0;
		List<Fund> fundList = fundMapper.selectSelective(fund);

		if(null!=fundList){
			fundNum =fundList.size();

		}
		index_result.put("fundNum",fundNum);
		OfficeSupplies officeSupplies = new OfficeSupplies();
		officeSupplies.setCommunityId(community_id);
		officeSupplies.setIsDelete(0);
		int officeSuppliesNum=0;
		List<OfficeSupplies> officeSuppliesList = officeSuppliesMapper.selectSelective(officeSupplies);

		if(null!=officeSuppliesList){
			officeSuppliesNum =officeSuppliesList.size();

		}
		index_result.put("officeSuppliesNum",officeSuppliesNum);

		index_result.put("projectNum",fundNum+officeSuppliesNum);

		
		if(index_List.size()>0){
			JSONArray jsa=new JSONArray();
			for(int i =0;i<index_List.size();i++){
				Map index_map=index_List.get(i);
				jsa.add(index_map);
			}


			index_result.put("data", jsa);

			
		}else{
			index_result.put("error", "没有记录！");
		}

		response.setContentType("application/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(index_result.toString());
	}
}
